package com.cn.demo4.servlet.admin.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.model.UserModel;
import com.cn.demo4.service.IUserService;
import com.cn.demo4.service.impl.UserServiceImpl;
import com.cn.demo4.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@WebServlet("/admin/user/edit")
public class UserEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.解决乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        //2.取得前台的值
        String id = req.getParameter("id");
        String userName = req.getParameter("userName");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String age = req.getParameter("age");
        String birthday = req.getParameter("birthday");
        String hobby = req.getParameter("hobby");
        String sex = req.getParameter("sex");
        //3.封装数据
        UserModel userModel = new UserModel();
        userModel.setId(Integer.valueOf(id));
        userModel.setUserName(userName);
        userModel.setPhone(phone);
        userModel.setEmail(email);
        userModel.setAge(Integer.valueOf(age));
        userModel.setBirthday(birthday);
        userModel.setHobby(hobby);
        userModel.setSex(sex);

        //4.调用服务逻辑层
        IUserService iUserService = new UserServiceImpl();

        //5.得到数据
        String result = iUserService.edit(userModel);

        //返回一个结果
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }
}
